package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.SchoolQuarter;
import com.xmy.cultivate.entity.response.ArrivalRate;
import com.xmy.cultivate.entity.views.StudyPlan;
import com.xmy.cultivate.service.IFinancialDataService;
import com.xmy.cultivate.service.ISchoolQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/financialData")
public class FinancialDataController extends BaseController {
    @Autowired
    IFinancialDataService iFinancialDataService;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @ApiOperation("教务数据列表")
    @GetMapping("getFinancialData")
    public Result getStudyPlanData(@RequestParam Map<String, String> reMap) {
        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        String type = reMap.get("type");
        String enterOrFollow = reMap.get("enterOrFollow");
        String schoolId = reMap.get("schoolId");
        String classify = reMap.get("classify");
        String trackType = reMap.get("trackType");
        String teamId = reMap.get("teamId");

        List<StudyPlan> studyPlanList = new ArrayList<>();

        if (type.equals("5")) {
            studyPlanList = iFinancialDataService.getStudyPlanTeamList(yearPart, quarterNum, enterOrFollow, schoolId, classify, trackType, teamId);
        } else {
            if (StringUtils.isBlank(schoolId)) {
                if (type.equals("2")) {
                    //校区老师状态，只能看自己所在校区的数据
                    schoolId = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
                }
            }
            studyPlanList = iFinancialDataService.getStudyPlanList(yearPart, quarterNum, type, enterOrFollow, schoolId);

            if (type.equals("6") && StringUtils.isBlank(schoolId)) {
                List<StudyPlan> collectList = new ArrayList<>();

                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("year_part", yearPart);
                queryWrapper.eq("quarter_num", quarterNum);
                queryWrapper.eq("pid", 0);
                List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

                for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                    StudyPlan studyPlan = new StudyPlan();
                    studyPlan.setSchoolId(schoolQuarter.getSchoolId());
                    studyPlan.setSchoolName(schoolQuarter.getSchoolName());
                    studyPlan.setSchoolPid(schoolQuarter.getPid());
                    studyPlan.setAllStudentCount(0);
                    studyPlan.setAlreadyShutNum(0);
                    studyPlan.setNotShutNum(0);

                    collectList.add(studyPlan);
                }

                for (StudyPlan plan : collectList) {
                    List<StudyPlan> childRates = new ArrayList<>();
                    for (StudyPlan studyPlan : studyPlanList) {
                        if (plan.getSchoolId().equals(studyPlan.getSchoolId()) || plan.getSchoolId().equals(studyPlan.getSchoolPid())) {
                            childRates.add(studyPlan);
                            if (studyPlan.getAllStudentCount() != null) {
                                plan.setAllStudentCount(plan.getAllStudentCount() + studyPlan.getAllStudentCount());
                            }
                            if (studyPlan.getAlreadyShutNum() != null) {
                                plan.setAlreadyShutNum(plan.getAlreadyShutNum() + studyPlan.getAlreadyShutNum());
                            }
                            if (studyPlan.getNotShutNum() != null) {
                                plan.setNotShutNum(plan.getNotShutNum() + studyPlan.getNotShutNum());
                            }
                        }
                    }
                    plan.setChildRateList(childRates);
                }

                studyPlanList = collectList;
            }
        }

        return new Result(Code.OK, studyPlanList, "获取教务数据列表成功");
    }

    @ApiOperation("教务数据详情")
    @GetMapping("getFinancialDataDetail")
    public Result getStudyPlanDataDetail(@RequestParam Map<String, String> reMap) {
        int pageNum = StringUtils.isBlank(reMap.get("pageNum")) ? 1 : Integer.parseInt(reMap.get("pageNum"));
        int pageSize = StringUtils.isBlank(reMap.get("pageSize")) ? 10 : Integer.parseInt(reMap.get("pageSize"));
        IPage<Grade> page = new Page<>(pageNum, pageSize);

        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        String gradeId = reMap.get("gradeId");
        String enterOrFollowState = reMap.get("enterOrFollowState");
        String enterOrFollow = reMap.get("enterOrFollow");
        String teacherId = reMap.get("teacherId");
        String schoolId = reMap.get("schoolId");
        String classify = reMap.get("classify");
        String trackType = reMap.get("trackType");
        String teamId = reMap.get("teamId");
        String type = reMap.get("type");

        if (type.equals("5")) {
            iFinancialDataService.getStudyPlanTeamDetails(page, yearPart, quarterNum, teacherId, enterOrFollow, enterOrFollowState, gradeId, schoolId, classify, trackType, teamId);
        } else {
            iFinancialDataService.getStudyPlanDetails(page, yearPart, quarterNum, teacherId, enterOrFollow, enterOrFollowState, gradeId, schoolId);
        }


        return new Result(Code.OK, new PageResult<>(page.getRecords(), page.getTotal(), page.getPages()), "获取教务数据详情成功");
    }
}
